package syncrized;

public class Demo {
    private static final Object o1 = new Object();

    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(() -> {
            System.out.println("t1: enter");
            synchronized (o1) {
                try {
                    System.out.println("t1: start lock");
                    Thread.sleep(5000);
                    System.out.println("t1: end lock");
                } catch (InterruptedException e) {
                    System.out.println("t1: interruptedException");
                    e.printStackTrace();
                }
            }
            System.out.println("t1: exit");

        });

        Thread thread2 = new Thread(() -> {
            System.out.println("t2: enter");
            synchronized (o1) {
                try {
                    System.out.println("t2: start lock");
                    Thread.sleep(1000);
                    System.out.println("t2: end lock");
                } catch (InterruptedException e) {
                    System.out.println("t2: interruptedException");
                    e.printStackTrace();
                }
            }
            System.out.println("t2: exit");
        });

        thread1.start();
        //让thread1先执行
        Thread.sleep(200);
        thread2.start();

        // 中断t2线程的执行
        thread2.interrupt();
        System.out.println("main: t2 interrupt...");
    }
}